﻿using System;
using System.Collections.Generic;
using Spring.Data.Core;
using System.Data;
using Spring.Data;

namespace TeamTasker.Server.DataAccess.Support
{
    public class DictionaryDAO : AdoDaoSupport
    {
        public Dictionary LoadDictionary(string dictName)
        {
            Dictionary dict = (Dictionary) AdoTemplate.QueryWithResultSetExtractor(
                CommandType.Text, 
                "select ID, Name from " + dictName, 
                new DictionaryResultSetExtractor());
            dict.Name = dictName;
            return dict;
        }
    }

    class DictionaryResultSetExtractor : IResultSetExtractor
    {
        public object ExtractData(IDataReader reader)
        {
            Dictionary dict = new Dictionary();
            while (reader.Read())
            {
                int code = reader.GetInt32(0);
                string label = reader.GetString(1);
                dict.Add(code, label);
            }
            return dict;
        }
    }
}
